package com.cyzy.config;// MinioConfig.java
import io.minio.MinioClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

@Configuration
@ConfigurationProperties(prefix = "minio") // 绑定minio前缀的配置
public class MinioConfig {
    private String url="http://123.60.66.194:9000";
    private String accessKey="root123456";
    private String secretKey="root123456";
    private String bucketName="mybucket";

    // 非静态getter和setter（必须）
    public String getUrl() { return url; }
    public void setUrl(String url) { this.url = url; }
    public String getAccessKey() { return accessKey; }
    public void setAccessKey(String accessKey) { this.accessKey = accessKey; }
    public String getSecretKey() { return secretKey; }
    public void setSecretKey(String secretKey) { this.secretKey = secretKey; }
    public String getBucketName() { return bucketName; }
    public void setBucketName(String bucketName) { this.bucketName = bucketName; }

    // 关键：定义MinioClient的Bean，供Spring容器管理
    @Bean
    public MinioClient minioClient() {
        // 校验配置参数非空（避免空指针）
        Assert.hasText(url, "MinIO的url不能为空，请检查application.yml配置");
        Assert.hasText(accessKey, "MinIO的access-key不能为空，请检查配置");
        Assert.hasText(secretKey, "MinIO的secret-key不能为空，请检查配置");

        // 创建并返回MinioClient实例
        return MinioClient.builder()
                .endpoint(url)
                .credentials(accessKey, secretKey)
                .build();
    }
}